<?php

/**
 * @author 
 * @copyright 2009
 */

class Tools{
	
	public static $out;
	
	public static $count = 0;
	
	public static function db_optimize(){
		
		$result = System::$db->query('SHOW TABLES');
		
		if($result){
			while($table = $result->fetch_row())
				{
					$otimize = System::$db->query('OPTIMIZE TABLE '.$table[0]);
					self::$count += 1;
					if($otimize){
						self::$out .= "\n<tr><td>".$table[0]."</td><td><img src=\"../libs/icons/tick.png\" alt=\"Success\" /></td></tr>";
					}else{
						self::$out .= "\n<tr><td>".$table[0]."</td><td><img src=\"../libs/icons/cancel.png\" alt=\"Error\" /></td></tr>";
					}
				}
		}
	}
	
	public static function db_repair(){
		
		$result = System::$db->query('SHOW TABLES');
		
		if($result){
			while($table = $result->fetch_row())
				{
					$otimize = System::$db->query('REPAIR TABLE '.$table[0]);
					self::$count += 1;
					if($otimize){
						self::$out .= "\n<tr><td>".$table[0]."</td><td><img src=\"../libs/icons/tick.png\" alt=\"Success\" /></td></tr>";
					}else{
						self::$out .= "\n<tr><td>".$table[0]."</td><td><img src=\"../libs/icons/cancel.png\" alt=\"Error\" /></td></tr>";
					}
				}
		}
	}
	
	public static function db_size(){
		
		$result = System::$db->query('SHOW TABLE STATUS');
		self::$count += 1;
		
		while($table = $result->fetch_array())
		{
			$total_size = ($table[ "Data_length" ] + 
                   $table[ "Index_length" ]) / 1024;
   			$size = sprintf("%.2f", $total_size);
   			self::$out .= "\n<tr><td>".$table['Name']."</td><td><img src=\"../libs/icons/database_table.png\" alt=\"\" /> <strong>".$size."</strong>kB</td></tr>";
    		
		}
		
	}
	
	public static function log_truncate(){
		$query = System::$db->query("TRUNCATE TABLE {prefix}log");
		self::$count += 1;
		if($query){
			self::$out .= "\n<tr><td>TRUNCATE TABLE {prefix}log</td><td><img src=\"../libs/icons/tick.png\" alt=\"Success\" /></td></tr>";
		}else{
			self::$out .= "\n<tr><td>TRUNCATE TABLE {prefix}log</td><td><img src=\"../libs/icons/cancel.png\" alt=\"Error\" /></td></tr>";
		}
	}
	
	public static function __toString(){
		echo self::$out;
		
		echo '<tr><td><p>'.Lang::getBackGlobal('tools_message1', array(self::$count)).'</p></td></tr>';
		echo "\n<tr><td><h3><a href=\"".System::$url."index.php?p=tools\">".Lang::getBackGlobal('back')."</a></h3></td></tr>\n";
	}
	
}

?>